package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.health.common.MessageConst;

import com.itheima.health.common.RedisConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MemberService;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class MobileValidateCodeController {

    @Autowired
    private RedisTemplate redisTemplate;




    @RequestMapping("/send")
    public Result send(String type, String telephone) {

        if(!StringUtils.isEmpty(telephone)) {//手机号不为空
            //生成随机的四位验证码

            Integer code = ValidateCodeUtils.generateValidateCode(4);
            log.info("我们要发送给用户的验证码是:" + code);
//
            // 我们要把手机号 和验证码 存储到 redis中  键 字符串  值 字符串
            String key = RedisConst.VALIDATE_CODE_PREFIX +"_"+type+"_"+telephone;


            redisTemplate.opsForValue().set(key, code, 2, TimeUnit.MINUTES);
            //   redisTemplate.boundValueOps(redisKey).set(code,2,TimeUnit.MINUTES)
            // 手机号:code   存储时间 2 单位 分钟
            //  字符串  --- code--用Integer的序列化形式 .toString()写出出去了
            return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
        }
        return new Result(false,MessageConst.SEND_VALIDATECODE_FAIL);
    }


}
